package cn.qdhlm.testrocketmq.producer;


import cn.qdhlm.testrocketmq.producer.ByteBuff;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;


/**
 * @Name:MyProducer
 * @description:自定义producer
 * @author: xinY
 **/
public class MyProducer {
    public static void main(String[] args) {
        try {
            go();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public  static void go() throws Exception{
        DefaultMQProducer producer = new DefaultMQProducer("myProducer");
//        producer.setNamesrvAddr("localhost:9876");
        producer.setNamesrvAddr("192.168.0.159:9876");
//        producer.setNamesrvAddr("192.168.0.158:9876");
        producer.start();
        System.out.println("producer start");


        for (int i = 0 ; i < 10000 ; i ++){
            ByteBuff buff = new ByteBuff();
            buff.add(("myproducer__").getBytes(StandardCharsets.UTF_8));
            buff.add((""+i).getBytes(StandardCharsets.UTF_8));

            Message msg = new Message("TopicTest","Tags",buff.getData());
            producer.send(msg);
            System.out.println("topic:"+msg.getTopic()+", tags:"+msg.getTags()+", body:"+new String(msg.getBody()));
        }
        producer.shutdown();
    }
}
